/**
 * 一些系统方法的拓展
 * Created by zhangluxin on 16/8/4.
 */

/**
 * 将一个节点添加到另一个节点上
 * @param {cc.Node} node 节点
 * @param {number} [z_order] 层级
 * @param {number|string} [tag] tag值
 */
cc.Node.prototype.addTo = function (node, z_order, tag) {
    node.addChild(this, z_order, tag);
};
/**
 * 显示节点
 */
cc.Node.prototype.show = function () {
    this.setVisible(true);
};
/**
 * 隐藏节点
 */
cc.Node.prototype.hide = function () {
    this.setVisible(false);
};
/**
 * 移动节点
 */
cc.Node.prototype.move = function () {
    var x = 0;
    var y = 0;
    if (arguments.length == 1) {
        x = arguments[0].x;
        y = arguments[0].y;
    } else {
        x = arguments[0];
        y = arguments[1];
    }
    this.x = x;
    this.y = y;
};

/**
 * 是否有效
 * @returns {boolean}
 */
cc.Node.prototype.isValid = function () {
    return cc.sys.isObjectValid(this);
};

/**
 * 继承加强版
 * @param props
 */
cc.Node.extendEX = function (props) {
    _.forOwn(props, function (value, key) {
        if (_.isFunction(value)) {
            if (key == 'ctor') {
                return;
            }
            props[key] = function () {
                try {
                    return value.apply(this, arguments);
                } catch (e) {
                    alertError("ui", e, null);
                }
            };
            props[key].funcBack = value;
        }
    });
    var ret = (this == window ? cc.Node : this).extend(props);
    ret.extendEX = cc.Node.extendEX;
    return ret;
};

cc.Layer.extendEX = function (props) {
    _.forOwn(props, function (value, key) {
        if (_.isFunction(value)) {
            if (key === 'ctor' || _.startsWith(key, 'onBefore') || _.startsWith(key, 'onAfter')) {
                return;
            }
            props[key] = function () {
                try {
                    var onBefore = 'onBefore' + key.charAt(0).toUpperCase() + key.substr(1);
                    var onAfter = 'onAfter' + key.charAt(0).toUpperCase() + key.substr(1);
                    if (this.__proto__[onBefore]) {
                        this[onBefore].apply(this, arguments);
                    }
                    var ret = value.apply(this, arguments);
                    if (this.__proto__[onAfter]) {
                        this[onAfter].apply(this, arguments);
                    }
                    return ret;
                } catch (e) {
                    alertError("ui", e, null);
                }
            };
            props[key].funcBack = value;
        }
    });
    var ret = (this == window ? cc.Layer : this).extend(props);
    ret.extendEX = cc.Layer.extendEX;
    return ret;
};

cc.ClippingNode.createRoundRect = function (width, height, radius) {
    var offsetX = 0;
    var offsetY = 0;
    var clipWidth = width;
    var clipHeight = height;
    var size = cc.size(width, height);
    var stencil = new cc.DrawNode();
    stencil.drawRect(cc.p((size.width - clipWidth) / 2 + offsetX, (size.height - clipHeight) / 2 + offsetY + radius), cc.p(size.width / 2 + clipWidth / 2 + offsetX, size.height / 2 + clipHeight / 2 + offsetY - radius), cc.color(0, 0, 0), 1, cc.color(0, 0, 0));
    stencil.drawRect(cc.p((size.width - clipWidth) / 2 + offsetX + radius, (size.height - clipHeight) / 2 + offsetY), cc.p(size.width / 2 + clipWidth / 2 + offsetX - radius, size.height / 2 + clipHeight / 2 + offsetY), cc.color(0, 0, 0), 1, cc.color(0, 0, 0));
    stencil.drawCircle(cc.p(size.width / 2 - clipWidth / 2 + offsetX + radius, size.height / 2 - clipHeight / 2 + offsetY + radius), radius / 3, 0, 100, false, radius, cc.color(0, 0, 0));
    stencil.drawCircle(cc.p(size.width / 2 + clipWidth / 2 + offsetX - radius, size.height / 2 - clipHeight / 2 + offsetY + radius), radius / 3, 0, 100, false, radius, cc.color(0, 0, 0));
    stencil.drawCircle(cc.p(size.width / 2 + clipWidth / 2 + offsetX - radius, size.height / 2 + clipHeight / 2 + offsetY - radius), radius / 3, 0, 100, false, radius, cc.color(0, 0, 0));
    stencil.drawCircle(cc.p(size.width / 2 - clipWidth / 2 + offsetX + radius, size.height / 2 + clipHeight / 2 + offsetY - radius), radius / 3, 0, 100, false, radius, cc.color(0, 0, 0));
    var clippingNode = new cc.ClippingNode();
    clippingNode.stencil = stencil;
    clippingNode.setAlphaThreshold(255);
    return clippingNode;
};

String.prototype.hashCode = function () {
    var hash = 0, i, chr;
    if (this.length === 0) return hash;
    for (i = 0; i < this.length; i++) {
        chr = this.charCodeAt(i);
        hash = ((hash << 5) - hash) + chr;
        hash |= 0; // Convert to 32bit integer
    }
    return hash;
};

String.prototype.bool = function() {
    return (/^true$/i).test(this);
};